package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;
import android.widget.TextView.OnEditorActionListener;

import com.itboye.textileexpes.beans.DetailBean;
import com.itboye.textileexpes.beans.GoodsDetailBean;
import com.itboye.textileexpes.beans.GoodsDetailBean.A;
import com.itboye.textileexpes.beans.ShopCartModel;
import com.itboye.textileexpes.presenter.GoodsPresenter;
import com.itboye.textileexpes.presenter.ShopCartPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.R.string;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.login.LoginActivity;
import com.itboye.textileexpres.main.MainActivity;
import com.itboye.textileexpres.myset.ActivityCustomerDetail;
import com.itboye.textileexpres.myset.ActivityCustomerService;
import com.itboye.textileexpres.util.BaseDialogFragment;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.PriceChange;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexpres.widget.CarouselView;
import com.itboye.textileexpres.widget.DertailServiceDialog;
import com.itboye.textileexpres.widget.MyListView;
import com.itboye.textileexpres.widget.RoundImageView;
import com.itboye.textileexptes.config.GlobalConfig;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.open.utils.Util;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

public class GoodsDetail extends BaseActivity implements Observer,
		OnClickListener {
	ImageView back;
	TextView title;
	ImageView img_other;
	ImageView img_search;
	PopupWindow popupWindow;
	PopupWindow window;
	TextView share_tv, new_tv, main_tv;
	LinearLayout shopcart_lin, buy_lin, order_lin, custom_lin;
	CarouselView topLunbo;
	private List<ImageView> bmps;
	TextView publish_time, Publish_people_name, collect_num, goods_num,
			goods_name, goods_detail, price_1_tv, limit_1, price_2_tv, limit_2,
			price_3_tv, limit_3, address_tv, color_tv, type_tv, service_tv,
			choose_tv;
	TextView product_attr, pinpai_tv, huohao_tv, caizhi_tv;
	LinearLayout sercice_lin, product_attr_liner;
	RoundImageView person_img;
	TextView name, phone;
	String pid;
	RelativeLayout top_rel;
	GoodsPresenter presenter;
	GoodsDetailBean detailBean;
	double total;
	int totalNum;
	String type;
	StringBuffer descid;
	StringBuffer moreCount;
	ScrollView scroll;
	LinearLayout bottom;
	ShopCartPresenter shopCartPresenter;
	View pop;
	static final String WXAPPID = "wx641da6617a5f5f67";
	static final String url = "http://api.guannan.itboye.com/public/web.php/Web/app_share?pid=";
	private String QQAPP_ID = "1105844486";
	Tencent mTencent;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_goodsdetail;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText("商品详情");
		pid = getIntent().getStringExtra("pid");
		showProgressDialog("正在加载中", true);
		mTencent = Tencent.createInstance(QQAPP_ID,
				this.getApplicationContext());
		scroll.setVisibility(View.GONE);
		bottom.setVisibility(View.GONE);
		presenter = new GoodsPresenter(this);
		shopCartPresenter = new ShopCartPresenter(this);
		presenter.DetailQuery("" + pid);
		Log.e("hu.detaile", pid + "");
		img_search.setImageResource(R.drawable.goods_shopcart);
		img_other.setImageResource(R.drawable.goods_more);
		img_other.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				View popView = LayoutInflater.from(GoodsDetail.this).inflate(
						R.layout.pop_goods_detail, null);
				share_tv = (TextView) popView.findViewById(R.id.share_tv);
				new_tv = (TextView) popView.findViewById(R.id.news_tv);
				main_tv = (TextView) popView.findViewById(R.id.main_tv);
				share_tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub

						// Toast(GoodsDetailsActivity.this, "share");
						Onshre();
						popupWindow.dismiss();
					}

				});
				new_tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(GoodsDetail.this,
								ActivityCustomerService.class);
						startActivity(intent);
						popupWindow.dismiss();
						finish();

					}
				});
				main_tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(GoodsDetail.this,
								MainActivity.class);
						startActivity(intent);
						popupWindow.dismiss();
						finish();
					}
				});
				popupWindow = new PopupWindow(popView, ChangeToDP(100),
						LayoutParams.WRAP_CONTENT);
				popupWindow.setOutsideTouchable(true);
				popupWindow.setBackgroundDrawable(new BitmapDrawable());
				popupWindow.showAsDropDown(v);

			}
		});
		custom_lin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(GoodsDetail.this,
						ActivityCustomerService.class);
				startActivity(intent);
				finish();
			}
		});
		img_search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent("gotoshopcart");
				intent.putExtra("index", 2);
				sendBroadcast(intent);
				finish();
			}
		});
		shopcart_lin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				type = "shopcar";
				new JiaRuGouWuCheDialog().show(getSupportFragmentManager(), "");
			}
		});
		color_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new JiaRuGouWuCheDialog().show(getSupportFragmentManager(), "");
			}
		});
		service_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new DertailServiceDialog(GoodsDetail.this).show(
						getSupportFragmentManager(), "");
			}
		});
		// onBaner();
	}

	private void Onshre() {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.item_addd_share, null);
		window = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.WRAP_CONTENT);
		window.setFocusable(true);
		window.setBackgroundDrawable(new BitmapDrawable());
		window.showAtLocation(findViewById(R.id.bottom), Gravity.BOTTOM, 0, 0);
		ImageView shareqq = (ImageView) view.findViewById(R.id.shareQQ);
		shareqq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// ByAlert.alert("微信好友分享");
				onQQ();
			}

		});
		ImageView shareWeiXinHaoYou = (ImageView) view
				.findViewById(R.id.shareWeChat);
		shareWeiXinHaoYou.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// ByAlert.alert("微信好友分享");
				onPengYou();
			}

		});
		ImageView imgSharePengYouQuan = (ImageView) view
				.findViewById(R.id.imgSharePengYouQuan);
		imgSharePengYouQuan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// ByAlert.alert("微信朋友圈分享");
				onPengYouQuan();
			}

		});
	}

	private void onQQ() {
		// TODO Auto-generated method stub
		final Bundle params = new Bundle();
		params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
				QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
		params.putString(QQShare.SHARE_TO_QQ_TITLE, "TextileExpress");
		params.putString(QQShare.SHARE_TO_QQ_SUMMARY,
				"我在TextileExpress发现了一个好玩的东西，速来围观");
		params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url + pid);
		params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,
				GlobalConfig.IMAGE_DOUBLE + detailBean.getMain_img());
		params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "TextileExpress");
		mTencent.shareToQQ(GoodsDetail.this, params, new IUiListener() {

			@Override
			public void onError(UiError arg0) {
				// TODO Auto-generated method stub
				Log.e("hu", arg0.errorMessage + "");
				Log.e("hu", arg0.toString() + "");
				Toast.makeText(getApplicationContext(), "分享失败，请重试", 0).show();
			}

			@Override
			public void onComplete(Object arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "分享完成", 0).show();
			}

			@Override
			public void onCancel() {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "已取消", 0).show();
			}
		});
	}

	private void onPengYou() {
		// TODO Auto-generated method stub
		final IWXAPI api = WXAPIFactory.createWXAPI(this, WXAPPID, true);
		api.registerApp(WXAPPID);
		api.openWXApp();
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = "" + url + pid;
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = "TextileExpress";
		msg.description = "我在TextileExpress发现了一个好玩的东西，速来围观";
		// Bitmap thumb = BitmapFactory.decodeResource(getResources(),
		// R.drawable.ste);
		// int WX_THUMB_SIZE = 30;
		// Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb, WX_THUMB_SIZE,
		// WX_THUMB_SIZE, true);
		// thumb.recycle();
		// msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = "webpage";
		req.message = msg;
		req.scene = SendMessageToWX.Req.WXSceneSession;
		api.sendReq(req);
	}

	private void onPengYouQuan() {
		// TODO Auto-generated method stub
		final IWXAPI api = WXAPIFactory.createWXAPI(this, WXAPPID, true);
		api.registerApp(WXAPPID);
		api.openWXApp();
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = "" + url + pid;
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = "TextileExpress";
		msg.description = "我在TextileExpress发现了一个好玩的东西，速来围观";
		// Bitmap thumb = BitmapFactory.decodeResource(
		// getResources(), R.drawable.logo);
		// msg.thumbData=Util.bmpToByteArray(thumb,true);
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = "webpage";
		req.message = msg;
		req.scene = SendMessageToWX.Req.WXSceneTimeline;
		api.sendReq(req);

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GoodsPresenter.Detail_success) {
				detailBean = (GoodsDetailBean) entity.getData();
				if (detailBean.getId() == null || "".equals(detailBean.getId())) {
					scroll.setVisibility(View.GONE);
					bottom.setVisibility(View.GONE);
				} else {
					try {
						closeProgressDialog();
					} catch (Exception e) {
						// TODO: handle exception
					}
					scroll.setVisibility(View.VISIBLE);
					bottom.setVisibility(View.VISIBLE);
					onBaner();
				}

			} else if (entity.getEventType() == GoodsPresenter.Detail_fail) {
				scroll.setVisibility(View.GONE);
				bottom.setVisibility(View.GONE);
			}
			if (entity.getEventType() == ShopCartPresenter.MoreShopCartSuccessEvent) {
				Intent intent = new Intent("updateshopcar");
				sendBroadcast(intent);
				toast("" + entity.getData());
			} else if (entity.getEventType() == ShopCartPresenter.MoreShopCartFailureEvent) {
				toast("" + entity.getData());
			}
		} else {
			scroll.setVisibility(View.GONE);
			bottom.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.buy_lin:
			type = "lijibuy";
			new JiaRuGouWuCheDialog().show(getSupportFragmentManager(), "");
			break;
		case R.id.order_lin:
			type = "order";
			new JiaRuGouWuCheDialog().show(getSupportFragmentManager(), "");
			break;
		case R.id.img_search:
			intent = new Intent(GoodsDetail.this, ActivityCustomerDetail.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	private void onBaner() {
		// TODO Auto-generated method stub
		publish_time
				.setText(TimeFormat.DateFormat(detailBean.getExpire_time()));
		Publish_people_name.setText(":" + detailBean.getContact_name() + "");
		name.setText("" + detailBean.getContact_name() + "");
		collect_num.setText(detailBean.getFavorite_cnt() + "");
		goods_num.setText("产品编号:" + detailBean.getProduct_code() + "");
		goods_name.setText(detailBean.getName() + "");
		goods_detail.setText(detailBean.getSynopsis() + "");
		phone.setText("联系方式：" + detailBean.getContact_way());
		Log.e("hu....skulist", detailBean.getSku_list() + "");
		if (detailBean.getSku_list().size() != 0) {
			if ("".equals(detailBean.getSku_list().get(0).getCnt2())
					|| "0".equals(detailBean.getSku_list().get(0).getCnt2())
					|| detailBean.getSku_list().get(0).getCnt2() == null) {
				price_2_tv.setVisibility(View.GONE);
				limit_2.setVisibility(View.GONE);
			}
			if ("".equals(detailBean.getSku_list().get(0).getCnt3())
					|| "0".equals(detailBean.getSku_list().get(0).getCnt3())) {
				price_3_tv.setVisibility(View.GONE);
				limit_3.setVisibility(View.GONE);
			}
			try {
				price_1_tv.setText(""
						+ PriceChange.Change(detailBean.getSku_list().get(0)
								.getPrice()));
				price_2_tv.setText(""
						+ PriceChange.Change(detailBean.getSku_list().get(0)
								.getPrice2()));
				price_3_tv.setText(""
						+ PriceChange.Change(detailBean.getSku_list().get(0)
								.getPrice3()));

			} catch (Exception e) {
				// TODO: handle exception
			}
			limit_1.setText("" + detailBean.getSku_list().get(0).getCnt1()
					+ "条起批");
			limit_2.setText("" + detailBean.getSku_list().get(0).getCnt2()
					+ "-" + detailBean.getSku_list().get(0).getCnt3());
			limit_3.setText(">" + detailBean.getSku_list().get(0).getCnt3()
					+ "");
		} else {
			price_1_tv.setVisibility(View.GONE);
			limit_1.setVisibility(View.GONE);
			price_2_tv.setVisibility(View.GONE);
			limit_2.setVisibility(View.GONE);
			price_3_tv.setVisibility(View.GONE);
			limit_3.setVisibility(View.GONE);
		}

		address_tv.setText(detailBean.getPlace_origin() + "");
		StringBuffer color = new StringBuffer();
		for (int i = 0; i < detailBean.getSku_list().size(); i++) {
			String desc = new String();
			desc = (detailBean.getSku_list().get(i).getSku_desc());
			color.append(detailBean.getSku_list().get(i).getSku_desc());

		}
		color_tv.setText("" + color + "");
		topLunbo = (CarouselView) findViewById(R.id.topLunbo);
		bmps = new ArrayList<ImageView>();
		for (int i = 0; i < detailBean.getCarousel_images().size(); i++) {
			ImageView imageView = new ImageView(this);
			imageView.setScaleType(ScaleType.FIT_XY);

			com.nostra13.universalimageloader.core.ImageLoader.getInstance()
					.displayImage(
							GlobalConfig.IMAGE_DOUBLE
									+ detailBean.getCarousel_images().get(i),
							imageView);
			bmps.add(imageView);

		}
		topLunbo.setImageBitmaps(bmps);
		for (int i = 0; i < detailBean.getProperties().size(); i++) {
			View view = LayoutInflater.from(GoodsDetail.this).inflate(
					R.layout.item_goods_shuxing, null);
			TextView tv1 = (TextView) view.findViewById(R.id.tv1);
			TextView tv2 = (TextView) view.findViewById(R.id.tv2);
			tv1.setText(detailBean.getProperties().get(i).getProp_name() + ":");
			tv2.setText(detailBean.getProperties().get(i).getProp_value_list()
					.get(0).getValue_name()
					+ "");
			product_attr_liner.addView(view);

		}
		XImageLoader.load(GlobalConfig.IMAGE_DOUBLE + detailBean.getUid(),
				person_img);
	}

	class JiaRuGouWuCheDialog extends BaseDialogFragment {
		View close;
		ImageView pic;
		TextView unit;
		TextView total_price, total_num2;
		TextView price;
		TextView goods_number_tv;
		ViewGroup guigeContainer;
		View ok;
		View jian;
		View jia;
		EditText input;
		boolean isLiJIGouMai;
		ViewGroup container;
		String imgUrl;
		protected String priceString;
		protected String skuId;
		List<String> pricelist = new ArrayList<String>();
		List<String> codelist = new ArrayList<String>();
		List<String> guigelist = new ArrayList<String>();
		List<String> countlist = new ArrayList<String>();
		List<String> imglist = new ArrayList<String>();
		ArrayList<TextView> hashMap = new ArrayList<TextView>();
		int num;
		double myprice;
		PopupWindow popupWindow;
		String singleNum;

		public JiaRuGouWuCheDialog() {

		}

		void DialogTopShow(int i) {

			price.setText(PriceChange.Change(detailBean.getSku_list().get(i)
					.getPrice()));
			goods_number_tv.setText("产品编号："
					+ detailBean.getSku_list().get(i).getProduct_code());
			XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
					+ detailBean.getSku_list().get(i).getSku_pkid(), pic);
			// detailBean.getSku_list().get(i).setSelected(true);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			return LayoutInflater.from(GoodsDetail.this).inflate(
					R.layout.dialog_add_shopcart, null);
		}

		private TextView createTitle(String s) {
			TextView textView = new TextView(GoodsDetail.this);
			textView.setText(s);
			textView.setTextColor(Color.BLACK);
			return textView;
		}

		public void close() {
			View v = getView().findViewById(R.id.container);
			ObjectAnimator animator = ObjectAnimator.ofFloat(v, "translationY",
					0, v.getHeight()).setDuration(300);
			animator.addListener(new AnimatorListener() {
				@Override
				public void onAnimationStart(Animator arg0) {
				}

				@Override
				public void onAnimationRepeat(Animator arg0) {
				}

				@Override
				public void onAnimationEnd(Animator arg0) {
					dismiss();
				}

				@Override
				public void onAnimationCancel(Animator arg0) {
					dismiss();
				}
			});
			animator.start();
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {

			super.onActivityCreated(savedInstanceState);

			if (savedInstanceState != null) {
				// goodsDetailBeans = (GoodsDetailsRootBean) savedInstanceState
				// .getSerializable("goodsDetailBeans");
				// isLiJIGouMai = savedInstanceState.getBoolean("isLiJIGouMai");
			}
			if (detailBean.getSku_list().size() != 0) {
				XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
						+ detailBean.getSku_list().get(0).getSku_pkid(), pic);
				price.setText(PriceChange.Change(detailBean.getSku_list()
						.get(0).getPrice()));
				unit.setText("/" + detailBean.getGoods_unit_name());
				goods_number_tv.setText("产品编号："
						+ detailBean.getSku_list().get(0).getProduct_code());
			} else {
				XImageLoader.load(
						GlobalConfig.IMAGE_DOUBLE + detailBean.getMain_img(),
						pic);
				price.setVisibility(View.GONE);
				unit.setVisibility(View.GONE);
				goods_number_tv.setVisibility(View.GONE);
			}
			for (int i = 0; i < detailBean.getSku_list().size(); i++) {
				detailBean.getSku_list().get(i).setSelected(false);
				View view = LayoutInflater.from(GoodsDetail.this).inflate(
						R.layout.item_detail_goods, null);
				TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
				TextView price_m = (TextView) view.findViewById(R.id.price_m);
				TextView kucun = (TextView) view.findViewById(R.id.kucun);
				TextView jian = (TextView) view.findViewById(R.id.jian);
				TextView jia = (TextView) view.findViewById(R.id.jia);
				final TextView input = (TextView) view.findViewById(R.id.input);
				price_m.setText(""
						+ PriceChange.Change(detailBean.getSku_list().get(i)
								.getPrice()
								+ ""));
				kucun.setText("库存"
						+ detailBean.getSku_list().get(i).getQuantity() + "");
				color_tv.setText(detailBean.getSku_list().get(i).getSku_desc()
						+ "");
				jian.setTag(i);
				jia.setTag(i);
				input.setTag(i);
				input.setText(detailBean.getSku_list().get(0).getCnt1() + "");
				hashMap.add(input);
				input.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(final View v) {
						// TODO Auto-generated method stub
						View view = LayoutInflater.from(GoodsDetail.this)
								.inflate(R.layout.goodsdetail_edit, null);
						TextView pop_jian = (TextView) view
								.findViewById(R.id.pop_jian);
						TextView pop_jia = (TextView) view
								.findViewById(R.id.pop_jia);
						TextView pop_ok = (TextView) view.findViewById(R.id.ok);
						final EditText pop_input = (EditText) view
								.findViewById(R.id.pop_input);

						pop_jian.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								String s = input.getText().toString();
								try {
									int c = Integer.parseInt(s);
									if (c > Integer.parseInt(detailBean
											.getSku_list().get(0).getCnt1())) {
										c--;
									}
									pop_input.setText(c + 1 + "");

									pop_input.setSelection(input.getText()
											.length());

								} catch (NumberFormatException e) {
									pop_input.setText(Integer
											.parseInt(detailBean.getSku_list()
													.get(0).getCnt1())
											+ "");
								}
							}
						});
						pop_jia.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								String s = input.getText().toString();
								try {
									int c = Integer.parseInt(s);
									if (c < Integer.parseInt(detailBean
											.getSku_list()
											.get((int) v.getTag())
											.getQuantity())) {
										c++;
									}
									pop_input.setText(c + 1 + "");
									pop_input.setSelection(input.getText()
											.length());

								} catch (NumberFormatException e) {
									pop_input.setText(Integer
											.parseInt(detailBean.getSku_list()
													.get(0).getCnt1())
											+ "");
								}
							}
						});
						pop_ok.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View view) {
								// TODO Auto-generated method stub
								singleNum = pop_input.getText() + "";
								if (Integer.parseInt(singleNum) > Integer
										.parseInt(detailBean.getSku_list()
												.get((int) v.getTag())
												.getQuantity())) {
									toast("超出库存");
									return;
								} else if (Integer.parseInt(singleNum) < Integer
										.parseInt(detailBean.getSku_list()
												.get(0).getCnt1())) {
									toast("不够起批数量");
									return;
								}
								input.setText(singleNum);
								detailBean.getSku_list().get((int) v.getTag())
										.setSelected(true);
								total = 0;
								totalNum = 0;
								for (int j = 0; j < hashMap.size(); j++) {
									total += Double.parseDouble(detailBean
											.getSku_list().get(j).getPrice())
											* Integer.parseInt(hashMap.get(j)
													.getText() + "");
									totalNum += Integer.parseInt(hashMap.get(j)
											.getText() + "");
								}
								total_price.setText(PriceChange.Change(total
										+ ""));
								total_num2.setText("" + totalNum);
								popupWindow.dismiss();
							}
						});
						popupWindow = new PopupWindow(view,
								LayoutParams.FILL_PARENT, ChangeToDP(145));
						popupWindow.setOutsideTouchable(false);
						popupWindow.setFocusable(true);
						popupWindow.setBackgroundDrawable(new BitmapDrawable());
						// popupWindow.showAsDropDown(v);
						int[] location = new int[2];
						v.getLocationOnScreen(location);
						popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
								location[0], location[1] - ChangeToDP(155));
					}
				});

				jian.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						DialogTopShow((int) v.getTag());

						String s = input.getText().toString();
						try {
							int c = Integer.parseInt(s);
							if (c > Integer.parseInt(detailBean.getSku_list()
									.get(0).getCnt1())) {
								c--;
							} else {
								toast("不够起批数量");
							}
							input.setText(c + "");

							// input.setSelection(input.getText().length());
							total = 0;
							totalNum = 0;
							for (int j = 0; j < hashMap.size(); j++) {
								total += Double.parseDouble(detailBean
										.getSku_list().get(j).getPrice())
										* Integer.parseInt(hashMap.get(j)
												.getText() + "");
								totalNum += Integer.parseInt(hashMap.get(j)
										.getText() + "");
								if ("0".equals(hashMap.get(j).getText() + "")) {
									detailBean.getSku_list().get(j)
											.setSelected(false);

								} else {
									detailBean.getSku_list()
											.get((int) v.getTag())
											.setSelected(true);
								}
							}
						} catch (NumberFormatException e) {
							input.setText("0");
						}
						total_price.setText(PriceChange.Change(total + ""));
						total_num2.setText("" + totalNum);
					}

				});
				jia.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub

						DialogTopShow((int) v.getTag());
						String s = input.getText().toString();
						try {
							int c = Integer.parseInt(s);
							if (c < Integer.parseInt(detailBean.getSku_list()
									.get(0).getQuantity())) {
								input.setText(c + 1 + "");
							} else {
								toast("超出库存");
							}
							total = 0;
							totalNum = 0;
							for (int j = 0; j < hashMap.size(); j++) {
								total += Double.parseDouble(detailBean
										.getSku_list().get(j).getPrice())
										* Integer.parseInt(hashMap.get(j)
												.getText() + "");

								totalNum += Integer.parseInt(hashMap.get(j)
										.getText() + "");
							}
							detailBean.getSku_list().get((int) v.getTag())
									.setSelected(true);
							total_price.setText(PriceChange.Change(total + ""));
							total_num2.setText("" + totalNum);
						} catch (NumberFormatException e) {
							input.setText("0");
						}
					}
				});
				guigeContainer.addView(view);
			}

			close.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					close();
				}
			});
			ok.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (IsMobileNumPhone.isUid() == null
							|| "".equals(IsMobileNumPhone.isUid())) {
						toast("还未登录");
						Intent intent = new Intent(GoodsDetail.this,
								LoginActivity.class);
						startActivity(intent);
						return;
					}
					if ("lijibuy".equals(type)) {
						Intent intent = new Intent(GoodsDetail.this,
								ActivityBuyNow.class);
						TransportData(intent);
						dismiss();
						// DetailBean bean = new DetailBean();
						// descid = new StringBuffer();
						// for (int i = 0; i < detailBean.getSku_list().size();
						// i++) {
						// if (detailBean.getSku_list().get(i).isSelected() ==
						// true) {
						// descid.append(","
						// + detailBean.getSku_list().get(i)
						// .getSku_pkid());
						// pricelist.add(detailBean.getSku_list().get(i)
						// .getPrice());
						// codelist.add(detailBean.getSku_list().get(i)
						// .getProduct_code());
						// guigelist.add(detailBean.getSku_list().get(i)
						// .getSku_desc());
						// imglist.add(detailBean.getSku_list().get(i)
						// .getSku_pkid());
						//
						// } else {
						//
						// }
						// }
						// for (int j = 0; j < hashMap.size(); j++) {
						// if (detailBean.getSku_list().get(j).isSelected() ==
						// true) {
						// countlist.add(hashMap.get(j).getText() + "");
						// }
						//
						// }
						// bean.setChandi(detailBean.getPlace_origin());
						// bean.setName(detailBean.getContact_name());
						// bean.setGoodsname(detailBean.getName());
						// bean.setSecondline(detailBean.getSecondary());
						// bean.setSingle_price(pricelist);
						// bean.setFaburen((detailBean.getContact_name()));
						// bean.setTotal(total + "");
						// bean.setCode(codelist);
						// bean.setCount(countlist);
						// bean.setDesc(guigelist);
						// bean.setImg(imglist);
						// if (countlist.size() == 1
						// && "0".equals(countlist.get(0))) {
						// return;
						// }
						// if (codelist.size() == 0) {
						// return;
						// }
						// bean.setPskid(descid.toString());
						// intent.putExtra("detail", bean);
						// startActivity(intent);
					} else if ("order".equals(type)) {
						Intent intent = new Intent(GoodsDetail.this,
								ActivityYiXiangDingdan.class);
						TransportData(intent);
						dismiss();
					} else if ("shopcar".equals(type)) {
						ShopCartModel model = new ShopCartModel();
						descid = new StringBuffer();
						moreCount = new StringBuffer();
						for (int i = 0; i < detailBean.getSku_list().size(); i++) {
							if (detailBean.getSku_list().get(i).isSelected() == true) {
								descid.append(","
										+ detailBean.getSku_list().get(i)
												.getSku_pkid());

								pricelist.add(detailBean.getSku_list().get(i)
										.getPrice());
								codelist.add(detailBean.getSku_list().get(i)
										.getProduct_code());
								guigelist.add(detailBean.getSku_list().get(i)
										.getSku_desc());
								imglist.add(detailBean.getSku_list().get(i)
										.getSku_pkid());
								if (Integer.parseInt(hashMap.get(i).getText()
										+ "") < Integer.parseInt(detailBean
										.getSku_list().get(i).getCnt1())) {
									toast("不够起批条件");
									return;
								} else if (Integer.parseInt(hashMap.get(i)
										.getText() + "") > Integer
										.parseInt(detailBean.getSku_list()
												.get(i).getQuantity())) {
									toast("超过内存");
									return;
								}
							}
						}
						for (int j = 0; j < hashMap.size(); j++) {
							if (detailBean.getSku_list().get(j).isSelected() == true) {
								moreCount.append("," + hashMap.get(j).getText()
										+ "");
							}

						}
						if (codelist.size() == 0) {
							toast("请至少选中一个");
							return;
						}
						model.setUid(IsMobileNumPhone.isUid());
						model.setSku_pkid(descid.substring(1, descid.length()));
						model.setId(detailBean.getId());
						model.setCount(moreCount.substring(1,
								moreCount.length()));
						shopCartPresenter.shopcartDuoAdd(model);
						dismiss();
					}

				}
			});

		}

		private void TransportData(Intent intent) {
			clearAllList();
			DetailBean bean = new DetailBean();
			descid = new StringBuffer();
			for (int i = 0; i < detailBean.getSku_list().size(); i++) {
				if (detailBean.getSku_list().get(i).isSelected() == true) {
					descid.append(","
							+ detailBean.getSku_list().get(i).getSku_pkid());
					pricelist.add(detailBean.getSku_list().get(i).getPrice());
					codelist.add(detailBean.getSku_list().get(i)
							.getProduct_code());
					guigelist
							.add(detailBean.getSku_list().get(i).getSku_desc());
					imglist.add(detailBean.getSku_list().get(i).getSku_pkid());
					if ("order".equals(type)) {

					} else {
						if (Integer.parseInt(hashMap.get(i).getText() + "") < Integer
								.parseInt(detailBean.getSku_list().get(i)
										.getCnt1())) {
							toast("不够起批条件");
							return;
						} else if (Integer.parseInt(hashMap.get(i).getText()
								+ "") > Integer.parseInt(detailBean
								.getSku_list().get(i).getQuantity())) {
							toast("超过内存");
							return;
						}
					}
				} else {
				}
			}
			for (int j = 0; j < hashMap.size(); j++) {
				if (detailBean.getSku_list().get(j).isSelected() == true) {
					countlist.add(hashMap.get(j).getText() + "");
				}

			}

			if (codelist.size() == 0) {
				toast("请至少选中一个");
				return;
			}
			bean.setUnit(detailBean.getGoods_unit_name());
			bean.setChandi(detailBean.getPlace_origin());
			bean.setName(detailBean.getContact_name());
			bean.setGoodsname(detailBean.getName());
			bean.setSecondline(detailBean.getSecondary());
			bean.setSingle_price(pricelist);
			bean.setFaburen((detailBean.getContact_name()));
			bean.setTotal(total + "");
			bean.setCode(codelist);
			bean.setCount(countlist);
			bean.setDesc(guigelist);
			bean.setImg(imglist);
			bean.setId(detailBean.getId());
			if (countlist.size() == 1 && "0".equals(countlist.get(0))) {
				return;
			}

			bean.setPskid(descid.substring(1, descid.length()));
			intent.putExtra("detail", bean);
			startActivity(intent);
		}

		private void clearAllList() {
			pricelist.clear();
			codelist.clear();
			guigelist.clear();
			countlist.clear();
			imglist.clear();
			codelist.clear();
		}

	}
}
